/*
 * Copyright 2015 guohuanwen
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.bcgtgjyb.huanwen.customview.mylibrary;

import ohos.agp.animation.Animator;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

/**
 * Created by Administrator on 2015/11/9.
 */
public class PPTVLoading extends Component implements Component.DrawTask {
    EventHandler eventHandler = new EventHandler(EventRunner.getMainEventRunner());
    private Paint paint1;
    private Paint paint2;
    private int color1 = Color.getIntColor("#ff0099cc");
    private int color2 = Color.getIntColor("#ff669900");

    private boolean isInit = false;
    private DataAnimatorValue valueAnimator;
    private float numb = 0;

    private boolean isStop = false;

    private int mR = 0;

    /**
     * 构造
     *
     * @param context 上下问
     * @param attrs 属性集
     */
    public PPTVLoading(Context context, AttrSet attrs) {
        super(context, attrs);
        addDrawTask(this::onDraw);
        paint1 = new Paint();
        paint2 = new Paint();
        paint1.setColor(new Color(color1));
        paint2.setColor(new Color(color2));
    }

    /**
     * 开始
     */
    public void start() {
        if (valueAnimator == null) {
            valueAnimator = getValueAnimator();
        } else {
            valueAnimator.start();
        }
        if (isStop == false) {
            eventHandler.postTask(new Runnable() {
                @Override
                public void run() {
                    start();
                    invalidate();
                }
            }, 10);
        }
    }

    /**
     * 结束
     */
    public void stop() {
        this.isStop = true;
    }

    private DataAnimatorValue getValueAnimator() {
        DataAnimatorValue animator = new DataAnimatorValue();
        animator.setStart(-1);
        animator.setEnd(1f);
        animator.setDuration(1500);
        animator.setCurveType(Animator.CurveType.LINEAR);
        animator.start();
        return animator;
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        if (!isInit) {
            isInit = true;
            mR = getWidth() / 8;
            start();
        }
        numb = (float) valueAnimator.getAnimatedValue();
        if (numb < 0) {
            canvas.drawCircle((float) ((getWidth() - 2d * mR) * (1d - Math.abs(numb)) + mR), (float) ((double)getHeight() / 2), mR - 5, paint2);
            canvas.drawCircle((float) ((getWidth() - 2d * mR) * Math.abs(numb) + mR), (float) (getHeight() / 2d), mR - 5 * (float) Math.abs(Math.abs(numb) - 0.8), paint1);
        } else {
            canvas.drawCircle((float) ((getWidth() - 2d * mR) * (1 - Math.abs(numb - 1)) + mR), (float) (getHeight() / 2d), mR - 5, paint1);
            canvas.drawCircle((float) ((getWidth() - 2d * mR) * Math.abs(numb - 1) + mR), (float) (getHeight() / 2d), mR - 5 * (float) Math.abs(Math.abs(numb) - 0.8), paint2);
        }
        if (valueAnimator.isRunning()) {
            invalidate();
        }
    }
}
